// Copyright (c) 2008 Matthew Johnson
// Licensed under the MIT License (see license.txt)
package actionml.tracing {
	public class TraceFilter {
		var _predicates:Object;
		public function TraceFilter(o:Object) {
			_predicates = o;
		}
		public function match(t:Trace):Boolean {
			for each (var p:String in _predicates.getPropertyNames()) {
				if (p["endsWith"]("Contains")) {
					if (!t[p.replace("Contains", "").replace(/^class/, "className")].contains(_predicates[p])) {
						return false;
					}
				} else if (p["endsWith"]("DoesNotContain")) {
					if (t[p.replace("DoesNotContain", "").replace(/^class/, "className")].contains(_predicates[p])) {
						return false;
					}
				} else if (p["endsWith"]("In")) {
					var found:Boolean = false;
					var traceClass:String = t[p.replace("In", "").replace(/^class/, "className")];
					for each (var s:String in _predicates[p].split(",")) {
						s = s.replace(/^\s*|\s*$/, "");
						if (traceClass == s) {
							found = true;
						}
					}
					return found;
				} else if (p["endsWith"]("NotIn")) {
 					found = false;
					traceClass = t[p.replace("NotIn", "").replace(/^class/, "className")];
					for each (s in _predicates[p].split(",")) {
						s = s.replace(/^\s*|\s*$/, "");
						if (traceClass == s) {
							found = true;
						}
					}
					return !found;
				} else if (p["endsWith"]("IsNot")) {
					if (t[p.replace("IsNot", "").replace(/^class/, "className")] == _predicates[p]) {
						return false;
					}
				} else {				
					if (t[p.replace(/^class/, "className")] != _predicates[p]) {
						return false;
					}
				}
			}
			return true;
		}
	}
}